Skip to content

GH-141148: Ensure decimal.getcontext() returns fresh copy.#151953

Open
nascheme wants to merge 2 commits into
python:mainfrom
nascheme:decimal_ctx_depth
Open

GH-141148: Ensure decimal.getcontext() returns fresh copy.#151953
nascheme wants to merge 2 commits into
python:mainfrom
nascheme:decimal_ctx_depth

Conversation

@nascheme

@nascheme nascheme commented Jun 22, 2026

Copy link
Copy Markdown
Member

Ensure that decimal.getcontext() returns a per-task copy of the decimal.Context so that mutations are isolated between asyncio tasks and threads when sys.flags.thread_inherit_context is set.

This change is required because decimal.Context instances are mutable. The contextvars.Context object uses an immutable data structure (HAMT) to store variable bindings. That ensures each task has its own set of contextvar bindings. However, it doesn't help if the contextvar value itself is mutated.

This is an alternative to GH-146482. In this PR, we add a "depth" counter to PyContext, incremented every time we copy the HAMT. Then, the decimal.Context() instance can use this to determine that getcontext() needs to copy the instance. This ensures each thread or asyncio task has it's own copy of the decimal.Context() instance.

This differs from GH-146482 as follows:

  • it avoids the potential problem of keeping context vars alive longer, via the ctx_vars_origin reference. Instead, we are just using a 64-bit integer counter here to track depth.
  • it avoids copying decimal.Context in the case that the context is reused (entered) many times, e.g. for asyncio tasks
  • it creates slightly more work for the _pydecimal and _decimal.c code, since it has to track the depth on its Context instance

We need a per-task copy of decimal.Context() so that mutations are
isolated between asyncio tasks and threads using
sys.flags.thread_inherit_context.  This is done by adding a "depth"
counter to PyContext and copying the decimal.Context() instance whenever
it doesn't match the depth of the current PyContext.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant